package com.cq.jackmar.rxdemo.demo;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.cq.jackmar.rxdemo.R;
import com.cq.jackmar.rxdemo.http.base.BaseResult;
import com.cq.jackmar.rxdemo.http.httphelper.HttpHelperUser;
import com.cq.jackmar.rxdemo.http.subscriber.IOnNextListener;
import com.cq.jackmar.rxdemo.http.subscriber.RcListSubscriber;
import com.cq.jackmar.rxdemo.view.rvlist.IOnRefreshListener;
import com.cq.jackmar.rxdemo.view.rvlist.RefreshUtil;
import com.cq.jackmar.rxdemo.view.rvlist.view.JRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity implements IOnRefreshListener {
    private static final String TAG = "MainActivity";
    @Bind(R.id.recyclerView)
    JRecyclerView mRecyclerView;

    private List<String> strings = new ArrayList<>();
    private CommonAdapter<String> mAdapter;
    private RefreshUtil refreshUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ButterKnife.bind(this);

        refreshUtil = new RefreshUtil(this, mRecyclerView);
        refreshUtil.setOnRefreshListener(this);
        refreshUtil.setDefaultDivider();

        mAdapter = new CommonAdapter<String>(this, R.layout.cell_list, strings) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
//                holder.setText(R.id.tv_text,)
            }
        };
        mRecyclerView.setAdapter(mAdapter);

        HttpHelperUser.getInstance().test(new RcListSubscriber<BaseResult>(new IOnNextListener<BaseResult>() {
            @Override
            public void onNext(BaseResult baseResult) {
                Log.e(TAG, "onNext: " + baseResult.getData());
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                strings.add("");
                mAdapter.notifyDataSetChanged();
            }
        }, MainActivity.this,refreshUtil));

    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshUtil.completeRefreshAndLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoad() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshUtil.completeRefreshAndLoad();
            }
        }, 1000);
    }
}
